package com.mc.designpattern.strategy.util;

import com.mc.annotation.OrderChannel;
import com.mc.designpattern.strategy.channelStrategy.ChannelStrategy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/***
 * @ClassName HandleChannelProcessor
 * @Description 策略核心功能, 获取所有策略注解的类型
 * @Author CC
 * @Date: 2019/10/3012:04
 * @Version 1.0
 */
@Component
public class HandleChannelProcessor implements ApplicationContextAware {
    @Autowired
    HandleChannelContext channelContext;
    /**
     * 获取所有的策略Beanclass 加入HandlerOrderContext属性中
     *
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        final Map<String, Object> annotationMap = applicationContext.getBeansWithAnnotation(OrderChannel.class);
        for (Object v : annotationMap.values()) {
            final Class<ChannelStrategy> beanClass = (Class<ChannelStrategy>) v.getClass();
            final int channel = beanClass.getAnnotation(OrderChannel.class).value();
            channelContext.strategyMapPut(channel, beanClass);

        }

    }

}
